package com.javafactroy.kunlong.bip.icp.context;

import cn.hutool.core.annotation.AnnotationUtil;
import com.javafactroy.kunlong.bip.icp.common.consts.AppType;
import com.javafactroy.kunlong.bip.icp.common.consts.EAppType;
import com.javafactroy.kunlong.bip.icp.context.connpool.dxpftp.DxpFtpTemplatePool;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Data
@Slf4j
@Component
public class AppContext implements ApplicationContextAware {
    Map<String, Boolean> mapNettyClient = new ConcurrentHashMap<>();
    EAppType appType;
    public static AppContext appContext = new AppContext();
    static ApplicationContext applicationContext;

    public static void init(Class cls) throws Exception {
        EAppType appType = AnnotationUtil.getAnnotationValue(cls, AppType.class);
        appContext.setAppType(appType);
        log.info("app.type = {}", appType);

    }

    @Override
    public void setApplicationContext(ApplicationContext ctxt) throws BeansException {
        applicationContext = ctxt;
    }

    public static <T> T getBean(Class<T> cls) {
        return applicationContext.getBean(cls);
    }

    public static DxpFtpTemplatePool getDxpFtpTemplatePool() {
        return getBean(DxpFtpTemplatePool.class);
    }


}
